package jwt

import (
	dto "dhuviewer/dto"
	"fmt"

	"github.com/dgrijalva/jwt-go/v4"
)

func CheckJwt(tokenString string) (bool, dto.JwtPayload) {
	token, _ := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
		// Don't forget to validate the alg is what you expect:
		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
			return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
		}
		// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
		return []byte("2toZTm48Uouef6rQrHVhnVn4X"), nil
	})
	if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
		fmt.Println(claims)
		return true, dto.JwtPayload{
			Username: claims["username"].(string),
			Uid:      claims["uid"].(string),
			Level:    claims["level"].(string),
		}
	} else {
		return false, dto.JwtPayload{}
	}
}
